Void fibo(int n) { if(n==0 || n==1) { return n; } return fibo(n-1)+fib...
Linear Recursion vs Tree Recursion
- Linear Recursion: In linear recursion, a function calls itself only once in each recursive call. The recursion progresses in a linear manner, where each recursive call leads to a single subsequent recursive call until the base case is reached. Examples of linear recursion include factorial and fibonacci functions.
- Tree Recursion: In tree recursion, a function calls itself multiple times in each recursive call. This leads to a branching structure, where each recursive call can further branch out into multiple recursive calls. Examples of tree recursion include functions that calculate the factorial of a number using multiple recursive calls.
Head Recursion
Head recursion is a type of linear recursion where the recursive call is made before any other operations in the function. In head recursion, the recursive call is the first line of code in the function, followed by other operations.
Analysis of the Given Function
The given function is an example of fibonacci recursion, which is a linear recursion. Let's analyze the function and check if it falls under head recursion.
```
void fibo(int n) {
if(n==0 || n==1) {
return n;
}
return fibo(n-1) + fibo(n-2);
}
```
1. Base Case: The function checks if the input `n` is equal to 0 or 1. If true, it directly returns the value of `n`. These are the base cases that terminate the recursion.
2. Recursive Calls: If the input `n` is not 0 or 1, the function makes two recursive calls - `fibo(n-1)` and `fibo(n-2)`. These calls are made before any other operations in the function, indicating that it falls under head recursion.
3. Return Statement: The function returns the sum of the values obtained from the recursive calls `fibo(n-1)` and `fibo(n-2)`.
Conclusion
In conclusion, the given function `fibo()` is an example of linear recursion. It specifically falls under head recursion, as the recursive calls are made before any other operations in the function.
To make sure you are not studying endlessly, EduRev has designed Computer Science Engineering (CSE) study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Computer Science Engineering (CSE).